<template>
  <div class="search">
    <input
      type="text"
      v-model="keyword"
      @input="change"
      :placeholder="placeholder"
      @keydown="onKeyDown"
    />
    <Icon
      title="搜索"
      name="sousuo"
      font-size="20"
      color="rgba(167, 190, 202, 1)"
      @click.native="clickSearch"
    />
  </div>
</template>

<script>
import Icon from '@/components/icon'

export default {
  props: {
    value: {
      type: [Number, String],
      default: ''
    },
    placeholder: {
      type: String,
      default: '关键词'
    }
  },
  components: {
    Icon
  },
  data() {
    return {
      keyword: ''
    }
  },
  watch: {
    value(val) {
      this.keyword = val
    }
  },
  methods: {
    change() {
      this.$emit('input', this.keyword)
    },
    onKeyDown(event) {
      if (event.code === 'Enter') {
        this.search()
      }
    },
    clickSearch() {
      this.search()
    },
    search() {
      this.$emit('search', this.keyword)
    }
  }
}
</script>

<style lang="scss" scoped>
.search {
  height: 30px;
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background: rgba(255, 255, 255, 0.2);
  border: 1px solid rgba(255, 255, 255, 0.2);
  padding: 0 6px;

  & > input {
    width: 100%;
    flex: 1;
    background: transparent;
    outline: none;
    border: none;
    color: #fff;
    font-size: 14px;

    &::-webkit-input-placeholder {
      color: #a7beca;
      font-size: 14px;
    }
  }
}
</style>
